public class Solution {
    public static void main(String[] args) {
        int[] arr = new int[]{7,6,0,2,2,3,5,8,1,7,9,17,88,19,99,21};
        //bubbleSort(arr);
        quickSort(arr,0,arr.length-1);
        for (int i:
             arr) {
            System.out.print(i+" ");
        }
    }
    //冒泡排序
    public static void bubbleSort(int[] nums) {
        for (int i = 0; i < nums.length-1; i++) {
            boolean flg = true;
            for (int j = 0; j < nums.length-1; j++) {
                if(nums[j] > nums[j+1]) {
                    flg = false;
                 swap(nums,j,j+1);
                }
            }
            if(flg) {
                break;
            }
        }
    }

    //快速排序
    public static void quickSort(int[] nums,int start,int end) {
        if(start >=  end) {
            return;
        }
        int left = start;
        int right = end;
        int base = nums[start];
        while(left < right) {
            //右指针走到比基准值小的位置
            while (left < right && nums[right] >= base) {
                right--;
            }
            nums[left] = nums[right];
            //左指针走到比基准值大的位置
            while(left < right && nums[left] <= base) {
                left++;
            }
            nums[right] = nums[left];
        }
        nums[right] = base;
        //排序左边
        quickSort(nums,start,left-1);
        //排序右边
        quickSort(nums,right+1,end);
    }
    private static void swap(int[] nums,int i,int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
